<?php


namespace app\middleware;

use app\BaseController;
use app\Code;
use app\service\AuthService;
use think\facade\Cache;
use think\Request;

class JwtAuth extends BaseController
{
    public function handle(Request $request, \Closure $next)
    {
        $token = $request->header('Authorization');

        if (empty($token)) return $this->returnJson([], Code::AUTH_EXCEPTION, '权限错误');

        $authService = new AuthService();

        $user = $authService->checkingRole($token, $request->method(),$request->root(), $request->rule()->getRule());

        if (empty($user)) return $this->returnJson([], Code::AUTH_EXCEPTION, '权限错误');

//      验证通过,将得到的用户id,放到请求信息中去,方便后续使用
        $request->user = $user;

        return $next($request);
    }

}